<!DOCTYPE html>
<html lang="en">
<head><!-- hexo injector head_begin start -->
        <link rel="preconnect" href="https://blog.fantasyke.cn/picx-images-hosting" crossorigin=""><!-- hexo injector head_begin end -->
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 7.3.0">

  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/uploads/avatar.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/uploads/avatar.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">
  <meta name="google-site-verification" content="0qBBGdG7vYORyT5XLZRUqQZ_y5kKYhgyUwNqjSIImg0">
  <meta name="msvalidate.01" content="63C90D0EC3DA6C31FD109132B7BEF502">

<link rel="stylesheet" href="/css/main.css">

<link rel="stylesheet" href="//fonts.googleapis.com/css?family=Zen+Maru+Gothic:300,300italic,400,400italic,700,700italic%7CZCOOL+XiaoWei:300,300italic,400,400italic,700,700italic&display=swap&subset=latin,latin-ext">

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.6.0/css/all.min.css" integrity="sha256-5eIC48iZUHmSlSUz9XtjRyK2mzQkHScZY1WdMaoz74E=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.31/fancybox/fancybox.css" integrity="sha256-gkQVf8UKZgQ0HyuxL/VnacadJ+D2Kox2TCEBuNQg5+w=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"blog.fantasyke.cn","root":"/","images":"/images","scheme":"Mist","darkmode":false,"version":"8.21.1","exturl":true,"sidebar":{"position":"right","width_expanded":350,"width_dual_column":240,"display":"post","padding":18,"offset":12},"hljswrap":true,"copycode":{"enable":true,"style":null},"fold":{"enable":true,"height":500},"bookmark":{"enable":true,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":"gitalk","storage":true,"lazyload":true,"nav":{"gitalk":{"text":"gitalk","order":-1}},"activeClass":"gitalk"},"stickytabs":false,"motion":{"enable":true,"async":false,"transition":{"menu_item":"fadeInDown","post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"i18n":{"placeholder":"Searching...","empty":"We didn't find any results for the search: ${query}","hits_time":"${hits} results found in ${time} ms","hits":"${hits} results found"},"path":"/search.xml","localsearch":{"enable":true,"top_n_per_article":1,"unescape":false,"preload":false}}</script><script src="/js/config.js"></script>

    <meta name="description" content="简介💡使用 Docker 的时候，定义 Dockerfile 文件，然后使用 docker build、docker run 等命令操作容器。">
<meta property="og:type" content="article">
<meta property="og:title" content="7.Docker Compose">
<meta property="og:url" content="https://blog.fantasyke.cn/posts/61442/index.html">
<meta property="og:site_name" content="Fantasy&#39;Ke Blog">
<meta property="og:description" content="简介💡使用 Docker 的时候，定义 Dockerfile 文件，然后使用 docker build、docker run 等命令操作容器。">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2023-04-11T01:46:02.000Z">
<meta property="article:modified_time" content="2024-12-17T06:36:35.378Z">
<meta property="article:author" content="Fantasy-ke">
<meta property="article:tag" content="Docker">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="https://blog.fantasyke.cn/posts/61442/">


<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"en","comments":true,"permalink":"https://blog.fantasyke.cn/posts/61442/","path":"posts/61442/","title":"7.Docker Compose"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>7.Docker Compose | Fantasy'Ke Blog</title>
  








  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
<link rel="alternate" href="/atom.xml" title="Fantasy'Ke Blog" type="application/atom+xml">
<style>.darkmode--activated{--body-bg-color:#282828;--content-bg-color:#333;--card-bg-color:#555;--text-color:#ccc;--blockquote-color:#bbb;--link-color:#ccc;--link-hover-color:#eee;--brand-color:#ddd;--brand-hover-color:#ddd;--table-row-odd-bg-color:#282828;--table-row-hover-bg-color:#363636;--menu-item-bg-color:#555;--btn-default-bg:#222;--btn-default-color:#ccc;--btn-default-border-color:#555;--btn-default-hover-bg:#666;--btn-default-hover-color:#ccc;--btn-default-hover-border-color:#666;--highlight-background:#282b2e;--highlight-foreground:#a9b7c6;--highlight-gutter-background:#34393d;--highlight-gutter-foreground:#9ca9b6}.darkmode--activated img{opacity:.75}.darkmode--activated img:hover{opacity:.9}.darkmode--activated code{color:#69dbdc;background:0 0}button.darkmode-toggle{z-index:9999}.darkmode-ignore,img{display:flex!important}.beian img{display:inline-block!important}</style></head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <div class="column">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="Toggle navigation bar" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">Fantasy'Ke Blog</p>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger" aria-label="Search" role="button">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>Home</a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>Archives</a></li><li class="menu-item menu-item-about"><a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>About</a></li><li class="menu-item menu-item-photos"><a href="/photos/" rel="section"><i class="fas fa-camera-retro fa-fw"></i>photos</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>Search
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
      <div class="search-header">
        <span class="search-icon">
          <i class="fa fa-search"></i>
        </span>
        <div class="search-input-container">
          <input autocomplete="off" autocapitalize="off" maxlength="80"
                placeholder="Searching..." spellcheck="false"
                type="search" class="search-input">
        </div>
        <span class="popup-btn-close" role="button">
          <i class="fa fa-times-circle"></i>
        </span>
      </div>
      <div class="search-result-container">
        <div class="search-result-icon">
          <i class="fa fa-spinner fa-pulse fa-5x"></i>
        </div>
      </div>
    </div>
  </div>

</header>
        
  
  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          Table of Contents
        </li>
        <li class="sidebar-nav-overview">
          Overview
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#docker-compose"><span class="nav-number">1.</span> <span class="nav-text"> Docker Compose</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#docker-compose-2"><span class="nav-number">2.</span> <span class="nav-text"> Docker Compose</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%AE%80%E4%BB%8B"><span class="nav-number">2.1.</span> <span class="nav-text"> 简介</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#yaml-%E5%AE%98%E6%96%B9%E7%A4%BA%E4%BE%8B"><span class="nav-number">2.1.1.</span> <span class="nav-text"> yaml 官方示例</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%AE%89%E8%A3%85"><span class="nav-number">2.2.</span> <span class="nav-text"> 安装</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%9F%A5%E7%9C%8B%E7%89%88%E6%9C%AC"><span class="nav-number">2.2.1.</span> <span class="nav-text"> 查看版本</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#docker-compose-version"><span class="nav-number">2.2.2.</span> <span class="nav-text"> docker-compose version</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%8D%B8%E8%BD%BD"><span class="nav-number">2.3.</span> <span class="nav-text"> 卸载</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%BD%BF%E7%94%A8"><span class="nav-number">2.4.</span> <span class="nav-text"> 使用</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%9E%84%E5%BB%BA"><span class="nav-number">2.4.1.</span> <span class="nav-text"> 构建</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%90%AF%E5%8A%A8"><span class="nav-number">2.4.2.</span> <span class="nav-text"> 启动</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#docker-compose-up"><span class="nav-number">2.4.3.</span> <span class="nav-text"> docker-compose up</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%81%9C%E6%AD%A2"><span class="nav-number">2.4.4.</span> <span class="nav-text"> 停止</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#docker-compose-stop"><span class="nav-number">2.4.5.</span> <span class="nav-text"> docker-compose stop</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%81%9C%E6%AD%A2%E5%B9%B6%E5%88%A0%E9%99%A4%E5%AE%B9%E5%99%A8%E5%92%8C%E7%BD%91%E7%BB%9C"><span class="nav-number">2.4.6.</span> <span class="nav-text"> 停止并删除容器和网络</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#docker-compose-down"><span class="nav-number">2.4.7.</span> <span class="nav-text"> docker-compose down</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%9B%B4%E5%A4%9A%E9%85%8D%E7%BD%AE"><span class="nav-number">2.5.</span> <span class="nav-text"> 更多配置</span></a></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="Fantasy-ke"
      src="/uploads/avatar.png">
  <p class="site-author-name" itemprop="name">Fantasy-ke</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">36</span>
          <span class="site-state-item-name">posts</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">6</span>
        <span class="site-state-item-name">categories</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">32</span>
        <span class="site-state-item-name">tags</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author animated">
      <span class="links-of-author-item">
        <span class="exturl" data-url="aHR0cHM6Ly9naXRodWIuY29tL2ZhbnRhc3kta2U=" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;fantasy-ke"><i class="fab fa-github fa-fw"></i>GitHub</span>
      </span>
      <span class="links-of-author-item">
        <span class="exturl" data-url="aHR0cHM6Ly94LmNvbS9BX0RyZWFtZXJfSGFyZA==" title="Twitter → https:&#x2F;&#x2F;x.com&#x2F;A_Dreamer_Hard"><i class="fab fa-twitter fa-fw"></i>Twitter</span>
      </span>
      <span class="links-of-author-item">
        <span class="exturl" data-url="aHR0cHM6Ly93d3cueW91dHViZS5jb20vQCVFNSU5MSVBOCVFNiVBMiVBNi16OWk=" title="YouTube → https:&#x2F;&#x2F;www.youtube.com&#x2F;@%E5%91%A8%E6%A2%A6-z9i"><i class="fab fa-youtube fa-fw"></i>YouTube</span>
      </span>
  </div>
<div class="cc-license animated" itemprop="sponsor">
  <a href="https://www.netlify.com" class="cc-opacity" title="Deploy with Netlify → https://www.netlify.com" target="_blank"><img width="80" src="https://www.netlify.com/img/global/badges/netlify-dark.svg" alt="Netlify"></a>
</div>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/aplayer@1.10.1/dist/APlayer.min.css">
<div id="aplayer" style="position:relative;left;0;bottom:0;"></div>
<script src="https://cdn.jsdelivr.net/npm/aplayer@1.10.1/dist/APlayer.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/color-thief-don@2.0.2/src/color-thief.js"></script>
<script>
  const ap = new APlayer({
    container: document.getElementById('aplayer'),
    autoplay: false, //自动播放
    listFolded: true, //播放列表默认折叠
    listMaxHeight: 90, //播放列表最大高度
    order: 'list', //音频循环顺序, 可选值: 'list', 'random'
    loop: 'all', //音频循环播放, 可选值: 'all', 'one', 'none'
    theme: '#e9e9e9', //切换音频时的主题色，优先级低于audio.theme
    preload: 'none', //音频预加载，可选值: 'none', 'metadata', 'auto'
    mutex: true, //互斥，阻止多个播放器同时播放，当前播放器播放时暂停其他播放器
    lrcType: 3, //歌词格式，可选值：3（LRC文件歌词格式），1（JS字符串歌词格式）
    volume: 0.7, //默认音量，请注意播放器会记忆用户设置，用户手动设置音量后默认音量即失效
    fixed: false, //吸底模式（fixed:true），迷你模式（mini:true），普通模式（注释此行或者设置fixed:false）
    audio: [{
        name: '平凡之路',
        artist: '朴树',
        lrc: '/downloads/lrc/平凡之路-朴树.lrc',
        cover: 'http://p2.music.126.net/W_5XiCv3rGS1-J7EXpHSCQ==/18885211718782327.jpg?param=300x300',
        url: 'http://ws.stream.qqmusic.qq.com/C4000007nRJ74eQsF6.m4a?guid=942726817&vkey=64755357C5716983BEECE2981FEC3954F0DD03E7630EFF769D9C40C31F50D53E7D260CB63E22CE8F954B1144083716A1BABFC2093F8F1C14&uin=&fromtag=120032'
      },
      {
        name: '后会无期',
        artist: '徐良,汪苏泷',
        lrc: '/downloads/lrc/后会无期-G.E.M.邓紫棋.lrc',
        cover: 'http://p1.music.126.net/vpvPajo3kn88nHc7jUjeWQ==/5974746185758035.jpg?param=300x300',
        url: 'http://ws.stream.qqmusic.qq.com/C400004ECnYg0QNchK.m4a?guid=893251117&vkey=D9DCBB8C4A4CAF8550E97A2A574DE1231DFDD8BC0989CC724742C7B9831C4108E0A805A46E0F2B2A556228467C146B9C312E3E8B498614C7&uin=&fromtag=120032'
      }
    ]
  });

  // 实现切换音频时，根据音频的封面图片自适应主题色
  const colorThief = new ColorThief();
  const setTheme = (index) => {
    if (!ap.list.audios[index].theme) {
      colorThief.getColorAsync(ap.list.audios[index].cover, function(color) {
        ap.theme(`rgb(${color[0]}, ${color[1]}, ${color[2]})`, index);
      });
    }
  };
  setTheme(ap.list.index);
  ap.on('listswitch', (data) => {
    setTheme(data.index);
  });
</script>



        </div>
      </div>
    </div>

    
    <div class="sidebar-inner sidebar-blogroll">
      <div class="links-of-blogroll animated">
        <div class="links-of-blogroll-title"><i class="fa fa-globe fa-fw"></i>
          Links
        </div>
        <ul class="links-of-blogroll-list">
            <li class="links-of-blogroll-item">
              <span class="exturl" data-url="aHR0cHM6Ly9mYW50YXN5LWtlLmdpdGh1Yi5pbw==" title="https:&#x2F;&#x2F;fantasy-ke.github.io">SunBlog</span>
            </li>
            <li class="links-of-blogroll-item">
              <span class="exturl" data-url="aHR0cHM6Ly9mYW50YXN5LWtlLmdpdGh1Yi5pbw==" title="https:&#x2F;&#x2F;fantasy-ke.github.io">做梦的努力者</span>
            </li>
            <li class="links-of-blogroll-item">
              <span class="exturl" data-url="aHR0cHM6Ly9naXRodWIuY29tL0ZhbnRhc3ktS2U=" title="https:&#x2F;&#x2F;github.com&#x2F;Fantasy-Ke">github</span>
            </li>
            <li class="links-of-blogroll-item">
              <span class="exturl" data-url="aHR0cHM6Ly9naXRlZS5jb20vZmFudGFzeV9rZQ==" title="https:&#x2F;&#x2F;gitee.com&#x2F;fantasy_ke">gitee</span>
            </li>
            <li class="links-of-blogroll-item">
              <span class="exturl" data-url="aHR0cHM6Ly93d3cuY25ibG9ncy5jb20vZmFudGFzeS1rZQ==" title="https:&#x2F;&#x2F;www.cnblogs.com&#x2F;fantasy-ke">我的园子</span>
            </li>
            <li class="links-of-blogroll-item">
              <span class="exturl" data-url="aHR0cHM6Ly9hYnAuaW8v" title="https:&#x2F;&#x2F;abp.io&#x2F;">abp</span>
            </li>
            <li class="links-of-blogroll-item">
              <span class="exturl" data-url="aHR0cHM6Ly9hYnAuaW8vZG9jcy9sYXRlc3Qv" title="https:&#x2F;&#x2F;abp.io&#x2F;docs&#x2F;latest&#x2F;">abp doc</span>
            </li>
            <li class="links-of-blogroll-item">
              <span class="exturl" data-url="aHR0cHM6Ly9ibG9ncy5tYXNhc3RhY2suY29tLw==" title="https:&#x2F;&#x2F;blogs.masastack.com&#x2F;">masa blog</span>
            </li>
        </ul>
      </div>
    </div>
        <div class="pjax">
        <div class="sidebar-inner sidebar-post-related">
          <div class="animated">
              <div class="links-of-blogroll-title"><i class="fa fa-signs-post fa-fw"></i>
    Related Posts
  </div>
  <ul class="popular-posts">
    <li class="popular-posts-item">
      <a class="popular-posts-link" href="/posts/43690/" rel="bookmark">
        <time class="popular-posts-time">2024-05-08</time>
        <br>
      Docker部署Mysql
      </a>
    </li>
    <li class="popular-posts-item">
      <a class="popular-posts-link" href="/posts/53249/" rel="bookmark">
        <time class="popular-posts-time">2023-04-11</time>
        <br>
      1.Docker介绍
      </a>
    </li>
    <li class="popular-posts-item">
      <a class="popular-posts-link" href="/posts/14184/" rel="bookmark">
        <time class="popular-posts-time">2023-04-11</time>
        <br>
      2.Docker基本使用
      </a>
    </li>
    <li class="popular-posts-item">
      <a class="popular-posts-link" href="/posts/31784/" rel="bookmark">
        <time class="popular-posts-time">2023-04-11</time>
        <br>
      3.Docker基本命令
      </a>
    </li>
    <li class="popular-posts-item">
      <a class="popular-posts-link" href="/posts/12360/" rel="bookmark">
        <time class="popular-posts-time">2023-04-11</time>
        <br>
      6.Docker网络
      </a>
    </li>
  </ul>

          </div>
        </div>
        </div>
  </aside>


    </div>

    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="en">
    <link itemprop="mainEntityOfPage" href="https://blog.fantasyke.cn/posts/61442/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/uploads/avatar.png">
      <meta itemprop="name" content="Fantasy-ke">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Fantasy'Ke Blog">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="7.Docker Compose | Fantasy'Ke Blog">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          7.Docker Compose
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">Posted on</span>

      <time title="Created: 2023-04-11 09:46:02" itemprop="dateCreated datePublished" datetime="2023-04-11T09:46:02+08:00">2023-04-11</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar-check"></i>
      </span>
      <span class="post-meta-item-text">Edited on</span>
      <time title="Modified: 2024-12-17 14:36:35" itemprop="dateModified" datetime="2024-12-17T14:36:35+08:00">2024-12-17</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">In</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/DevOps/" itemprop="url" rel="index"><span itemprop="name">DevOps</span></a>
        </span>
    </span>

  
    <span class="post-meta-item" title="Views" id="busuanzi_container_page_pv">
      <span class="post-meta-item-icon">
        <i class="far fa-eye"></i>
      </span>
      <span class="post-meta-item-text">Views: </span>
      <span id="busuanzi_value_page_pv"></span>
    </span>
    <span class="post-meta-break"></span>
    <span class="post-meta-item" title="Word count in article">
      <span class="post-meta-item-icon">
        <i class="far fa-file-word"></i>
      </span>
      <span class="post-meta-item-text">Word count in article: </span>
      <span>2.3k</span>
    </span>
    <span class="post-meta-item" title="Reading time">
      <span class="post-meta-item-icon">
        <i class="far fa-clock"></i>
      </span>
      <span class="post-meta-item-text">Reading time &asymp;</span>
      <span>8 mins.</span>
    </span>
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody"><h1 id="docker-compose"><a class="markdownIt-Anchor" href="#docker-compose"></a> Docker Compose</h1>
<h1 id="docker-compose-2"><a class="markdownIt-Anchor" href="#docker-compose-2"></a> Docker Compose</h1>
<h2 id="简介"><a class="markdownIt-Anchor" href="#简介"></a> 简介</h2>
<p>使用 <strong>Docker</strong> 的时候，定义 <strong>Dockerfile</strong> 文件，然后使用 <code>docker build</code>、<code>docker run</code> 等命令操作容器。</p>
<p>然而微服务架构的应用系统一般包含若干个微服务，每个微服务一般都会部署多个实例，如果每个微服务都要手动启停，这样效率很低，也不方便管理。</p>
<blockquote>
<p>使用 Docker Compose 可以轻松、高效的管理容器，它是一个用于定义和运行多容器 Docker 的应用程序工具。</p>
</blockquote>
<h3 id="yaml-官方示例"><a class="markdownIt-Anchor" href="#yaml-官方示例"></a> yaml 官方示例</h3>
<p><span class="exturl" data-url="aHR0cHM6Ly9kb2NzLmRvY2tlci5jb20vY29tcG9zZS9jb21wb3NlLWZpbGUvY29tcG9zZS1maWxlLXYzLyNjb21wb3NlLWZpbGUtc3RydWN0dXJlLWFuZC1leGFtcGxlcw==">https://docs.docker.com/compose/compose-file/compose-file-v3/#compose-file-structure-and-examples<i class="fa fa-external-link-alt"></i></span></p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br></pre></td><td class="code"><pre><span class="line">version: <span class="string">&quot;3.9&quot;</span></span><br><span class="line">services:</span><br><span class="line">  redis:</span><br><span class="line">    image: redis:alpine</span><br><span class="line">    ports:</span><br><span class="line">      - <span class="string">&quot;6379&quot;</span></span><br><span class="line">    networks:</span><br><span class="line">      - frontend</span><br><span class="line">    deploy:</span><br><span class="line">      replicas: <span class="number">2</span></span><br><span class="line">      update_config:</span><br><span class="line">        parallelism: <span class="number">2</span></span><br><span class="line">        delay: <span class="number">10</span>s</span><br><span class="line">      restart_policy:</span><br><span class="line">        condition: on<span class="literal">-failure</span></span><br><span class="line">  db:</span><br><span class="line">    image: postgres:<span class="number">9.4</span></span><br><span class="line">    volumes:</span><br><span class="line">      - db<span class="literal">-data</span>:/var/lib/postgresql/<span class="keyword">data</span></span><br><span class="line">    networks:</span><br><span class="line">      - backend</span><br><span class="line">    deploy:</span><br><span class="line">      placement:</span><br><span class="line">        max_replicas_per_node: <span class="number">1</span></span><br><span class="line">        constraints:</span><br><span class="line">          - <span class="string">&quot;node.role==manager&quot;</span></span><br><span class="line">  vote:</span><br><span class="line">    image: dockersamples/examplevotingapp_vote:before</span><br><span class="line">    ports:</span><br><span class="line">      - <span class="string">&quot;5000:80&quot;</span></span><br><span class="line">    networks:</span><br><span class="line">      - frontend</span><br><span class="line">    depends_on:</span><br><span class="line">      - redis</span><br><span class="line">    deploy:</span><br><span class="line">      replicas: <span class="number">2</span></span><br><span class="line">      update_config:</span><br><span class="line">        parallelism: <span class="number">2</span></span><br><span class="line">      restart_policy:</span><br><span class="line">        condition: on<span class="literal">-failure</span></span><br><span class="line">  result:</span><br><span class="line">    image: dockersamples/examplevotingapp_result:before</span><br><span class="line">    ports:</span><br><span class="line">      - <span class="string">&quot;5001:80&quot;</span></span><br><span class="line">    networks:</span><br><span class="line">      - backend</span><br><span class="line">    depends_on:</span><br><span class="line">      - db</span><br><span class="line">    deploy:</span><br><span class="line">      replicas: <span class="number">1</span></span><br><span class="line">      update_config:</span><br><span class="line">        parallelism: <span class="number">2</span></span><br><span class="line">        delay: <span class="number">10</span>s</span><br><span class="line">      restart_policy:</span><br><span class="line">        condition: on<span class="literal">-failure</span></span><br><span class="line">  worker:</span><br><span class="line">    image: dockersamples/examplevotingapp_worker</span><br><span class="line">    networks:</span><br><span class="line">      - frontend</span><br><span class="line">      - backend</span><br><span class="line">    deploy:</span><br><span class="line">      mode: replicated</span><br><span class="line">      replicas: <span class="number">1</span></span><br><span class="line">      labels: [<span class="type">APP</span>=<span class="type">VOTING</span>]</span><br><span class="line">      restart_policy:</span><br><span class="line">        condition: on<span class="literal">-failure</span></span><br><span class="line">        delay: <span class="number">10</span>s</span><br><span class="line">        max_attempts: <span class="number">3</span></span><br><span class="line">        window: <span class="number">120</span>s</span><br><span class="line">      placement:</span><br><span class="line">        constraints:</span><br><span class="line">          - <span class="string">&quot;node.role==manager&quot;</span></span><br><span class="line">  visualizer:</span><br><span class="line">    image: dockersamples/visualizer:stable</span><br><span class="line">    ports:</span><br><span class="line">      - <span class="string">&quot;8080:8080&quot;</span></span><br><span class="line">    stop_grace_period: <span class="number">1</span>m30s</span><br><span class="line">    volumes:</span><br><span class="line">      - <span class="string">&quot;/var/run/docker.sock:/var/run/docker.sock&quot;</span></span><br><span class="line">    deploy:</span><br><span class="line">      placement:</span><br><span class="line">        constraints:</span><br><span class="line">          - <span class="string">&quot;node.role==manager&quot;</span></span><br><span class="line">networks:</span><br><span class="line">  frontend:</span><br><span class="line">  backend:</span><br><span class="line">volumes:</span><br><span class="line">  db<span class="literal">-data</span>:</span><br></pre></td></tr></table></figure>
<p><strong>depends_on</strong>：依赖关系，如 web 依赖 redis 和 db，通过 depends_on 表明关系。</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">version: <span class="string">&quot;3.9&quot;</span></span><br><span class="line">services:</span><br><span class="line">  web:</span><br><span class="line">    build: .</span><br><span class="line">    depends_on:</span><br><span class="line">      - db</span><br><span class="line">      - redis</span><br><span class="line">  redis:</span><br><span class="line">    image: redis</span><br><span class="line">  db:</span><br><span class="line">    image: postgres</span><br></pre></td></tr></table></figure>
<h2 id="安装"><a class="markdownIt-Anchor" href="#安装"></a> 安装</h2>
<p>Docker Compose 是 Docker 的一个开源项目，目前托管到了 GitHub，需要前往 GitHub 下载。</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">sudo <span class="built_in">curl</span> <span class="literal">-L</span> <span class="string">&quot;https://github.com/docker/compose/releases/download/2.2.3/docker-compose-<span class="variable">$</span>(uname -s)-<span class="variable">$</span>(uname -m)&quot;</span> <span class="literal">-o</span> /usr/local/bin/docker<span class="literal">-compose</span></span><br><span class="line"></span><br><span class="line">chmod +x /usr/local/bin/docker<span class="literal">-compose</span></span><br></pre></td></tr></table></figure>
<blockquote>
<p>由于存放在 GitHub，国内网络限制导致不太稳定，不推荐使用。</p>
</blockquote>
<p>推荐使用 <span class="exturl" data-url="aHR0cHM6Ly93d3cuZGFvY2xvdWQuaW8v">道客<i class="fa fa-external-link-alt"></i></span> 提供的 <span class="exturl" data-url="aHR0cDovL2dldC5kYW9jbG91ZC5pby8jaW5zdGFsbC1jb21wb3Nl">Docker 极速下载<i class="fa fa-external-link-alt"></i></span> 进行安装。</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">curl</span> <span class="literal">-L</span> https://get.daocloud.io/docker/compose/releases/download/v2.<span class="number">2.3</span>/docker<span class="literal">-compose-</span>`uname <span class="literal">-s</span>`-`uname <span class="literal">-m</span>` &gt; /usr/local/bin/docker<span class="literal">-compose</span></span><br><span class="line"></span><br><span class="line">chmod +x /usr/local/bin/docker<span class="literal">-compose</span></span><br></pre></td></tr></table></figure>
<blockquote>
<p>安装</p>
</blockquote>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">[<span class="type">root</span>@<span class="type">sail</span> ~]<span class="comment"># curl -L https://get.daocloud.io/docker/compose/releases/download/v2.2.3/docker-compose-`uname -s`-`uname -m` &gt; /usr/local/bin/docker-compose</span></span><br><span class="line">  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current</span><br><span class="line">                                 Dload  Upload   Total   Spent    Left  Speed</span><br><span class="line"><span class="number">100</span>   <span class="number">423</span>  <span class="number">100</span>   <span class="number">423</span>    <span class="number">0</span>     <span class="number">0</span>    <span class="number">394</span>      <span class="number">0</span>  <span class="number">0</span>:<span class="number">00</span>:<span class="number">01</span>  <span class="number">0</span>:<span class="number">00</span>:<span class="number">01</span> <span class="literal">--</span>:<span class="literal">--</span>:<span class="literal">--</span>   <span class="number">394</span></span><br><span class="line"><span class="number">100</span> <span class="number">23.5</span>M  <span class="number">100</span> <span class="number">23.5</span>M    <span class="number">0</span>     <span class="number">0</span>  <span class="number">8670</span>k      <span class="number">0</span>  <span class="number">0</span>:<span class="number">00</span>:<span class="number">02</span>  <span class="number">0</span>:<span class="number">00</span>:<span class="number">02</span> <span class="literal">--</span>:<span class="literal">--</span>:<span class="literal">--</span> <span class="number">20.3</span>M</span><br><span class="line"></span><br><span class="line">[<span class="type">root</span>@<span class="type">sail</span> ~]<span class="comment"># cd /usr/local/bin/</span></span><br><span class="line">[<span class="type">root</span>@<span class="type">sail</span> <span class="type">bin</span>]<span class="comment"># ls</span></span><br><span class="line">docker<span class="literal">-compose</span></span><br></pre></td></tr></table></figure>
<p>这样即表示安装成功。</p>
<blockquote>
<p>授权</p>
</blockquote>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">[<span class="type">root</span>@<span class="type">sail</span> ~]<span class="comment"># chmod +x /usr/local/bin/docker-compose</span></span><br><span class="line">[<span class="type">root</span>@<span class="type">sail</span> ~]<span class="comment">#</span></span><br></pre></td></tr></table></figure>
<h3 id="查看版本"><a class="markdownIt-Anchor" href="#查看版本"></a> 查看版本</h3>
<h3 id="docker-compose-version"><a class="markdownIt-Anchor" href="#docker-compose-version"></a> docker-compose version</h3>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">[<span class="type">root</span>@<span class="type">sail</span> <span class="type">bin</span>]<span class="comment"># docker-compose version</span></span><br><span class="line">Docker Compose version v2.<span class="number">2.3</span></span><br></pre></td></tr></table></figure>
<p>显示了版本即代表 Docker Compose 安装完成。</p>
<h2 id="卸载"><a class="markdownIt-Anchor" href="#卸载"></a> 卸载</h2>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">rm</span> /usr/local/bin/docker<span class="literal">-compose</span></span><br></pre></td></tr></table></figure>
<p>由于 <strong>Linux 一切皆文件</strong>，删除此文件夹即可完成 Docker Compose 的卸载。</p>
<h2 id="使用"><a class="markdownIt-Anchor" href="#使用"></a> 使用</h2>
<h3 id="构建"><a class="markdownIt-Anchor" href="#构建"></a> 构建</h3>
<blockquote>
<p>创建项目目录</p>
</blockquote>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">[<span class="type">root</span>@<span class="type">sail</span> <span class="type">sail</span>]<span class="comment"># mkdir docker-compose</span></span><br><span class="line">[<span class="type">root</span>@<span class="type">sail</span> <span class="type">sail</span>]<span class="comment"># cd docker-compose</span></span><br><span class="line">[<span class="type">root</span>@<span class="type">sail</span> <span class="type">docker</span>-<span class="type">compose</span>]<span class="comment">#</span></span><br></pre></td></tr></table></figure>
<blockquote>
<p>创建 <span class="exturl" data-url="aHR0cDovL2FwcC5weQ==">app.py<i class="fa fa-external-link-alt"></i></span></p>
</blockquote>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line">[<span class="type">root</span>@<span class="type">sail</span> <span class="type">sail</span>]<span class="comment"># mkdir docker-compose</span></span><br><span class="line">[<span class="type">root</span>@<span class="type">sail</span> <span class="type">sail</span>]<span class="comment"># cd docker-compose</span></span><br><span class="line">[<span class="type">root</span>@<span class="type">sail</span> <span class="type">docker</span>-<span class="type">compose</span>]<span class="comment">#</span></span><br><span class="line"></span><br><span class="line">import time</span><br><span class="line">import redis</span><br><span class="line">from flask import Flask</span><br><span class="line">app = Flask(__name__)</span><br><span class="line">cache = redis.Redis(host=<span class="string">&#x27;redis&#x27;</span>, port=<span class="number">6379</span>)</span><br><span class="line">def get_hit_count():</span><br><span class="line">    retries = <span class="number">5</span></span><br><span class="line">    <span class="keyword">while</span> True:</span><br><span class="line">        <span class="keyword">try</span>:</span><br><span class="line">            <span class="keyword">return</span> cache.incr(<span class="string">&#x27;hits&#x27;</span>)</span><br><span class="line">        except redis.exceptions.ConnectionError as exc:</span><br><span class="line">            <span class="keyword">if</span> retries == <span class="number">0</span>:</span><br><span class="line">                raise exc</span><br><span class="line">            retries -= <span class="number">1</span></span><br><span class="line">            time.sleep(<span class="number">0.5</span>)</span><br><span class="line">@app.route(<span class="string">&#x27;/&#x27;</span>)</span><br><span class="line">def hello():</span><br><span class="line">    count = get_hit_count()</span><br><span class="line">    <span class="keyword">return</span> <span class="string">&#x27;Hello World! I have been seen &#123;&#125; times.\n&#x27;</span>.format(count)</span><br></pre></td></tr></table></figure>
<p>redis 是应用容器中 redis 容器的主机名，在同一网络下可以通过服务名访问，端口默认 6379。</p>
<blockquote>
<p>创建 requirements.txt</p>
</blockquote>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">[<span class="type">root</span>@<span class="type">sail</span> <span class="type">docker</span>-<span class="type">compose</span>]<span class="comment"># vim requirements.txt</span></span><br><span class="line">[<span class="type">root</span>@<span class="type">sail</span> <span class="type">docker</span>-<span class="type">compose</span>]<span class="comment"># cat requirements.txt </span></span><br><span class="line">flask</span><br><span class="line">redis</span><br></pre></td></tr></table></figure>
<blockquote>
<p>创建 Dockerfile</p>
</blockquote>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">[<span class="type">root</span>@<span class="type">sail</span> <span class="type">docker</span>-<span class="type">compose</span>]<span class="comment"># vim Dockerfile</span></span><br><span class="line"></span><br><span class="line">FROM python:<span class="number">3.7</span><span class="literal">-alpine</span></span><br><span class="line">WORKDIR /code</span><br><span class="line">ENV FLASK_APP=app.py</span><br><span class="line">ENV FLASK_RUN_HOST=<span class="number">0.0</span>.<span class="number">0.0</span></span><br><span class="line">RUN apk add <span class="literal">--no-cache</span> gcc musl<span class="literal">-dev</span> linux<span class="literal">-headers</span></span><br><span class="line"><span class="built_in">COPY</span> requirements.txt requirements.txt</span><br><span class="line">RUN pip install <span class="literal">-r</span> requirements.txt</span><br><span class="line">EXPOSE <span class="number">5000</span></span><br><span class="line"><span class="built_in">COPY</span> . .</span><br><span class="line">CMD [<span class="string">&quot;flask&quot;</span>, <span class="string">&quot;run&quot;</span>]</span><br></pre></td></tr></table></figure>
<blockquote>
<p>创建 docker-compose.yml</p>
</blockquote>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">[<span class="type">root</span>@<span class="type">sail</span> <span class="type">docker</span>-<span class="type">compose</span>]<span class="comment"># vim docker-compose.yml</span></span><br><span class="line"></span><br><span class="line">version: <span class="string">&quot;3.3&quot;</span></span><br><span class="line">services:</span><br><span class="line">  web:</span><br><span class="line">    build: .</span><br><span class="line">    ports:</span><br><span class="line">      - <span class="string">&quot;5000:5000&quot;</span></span><br><span class="line">  redis:</span><br><span class="line">    image: <span class="string">&quot;redis:alpine&quot;</span></span><br></pre></td></tr></table></figure>
<p>这个文件定义了两个服务：web 和 redis。</p>
<h3 id="启动"><a class="markdownIt-Anchor" href="#启动"></a> 启动</h3>
<h3 id="docker-compose-up"><a class="markdownIt-Anchor" href="#docker-compose-up"></a> docker-compose up</h3>
<blockquote>
<p>运行应用</p>
</blockquote>
<p>在项目目录中，运行 <code>docker-compose up</code> 来启动应用程序。</p>
<blockquote>
<p>第一次启动需要安装很多环境，比较缓慢。</p>
</blockquote>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br></pre></td><td class="code"><pre><span class="line">[<span class="type">root</span>@<span class="type">sail</span> <span class="type">docker</span>-<span class="type">compose</span>]<span class="comment"># docker-compose up</span></span><br><span class="line">[+] Running <span class="number">7</span>/<span class="number">7</span></span><br><span class="line"> ⠿ redis Pulled                                                                                                                                                                                              <span class="number">14.1</span>s</span><br><span class="line">   ⠿ <span class="number">59</span>bf1c3509f3 Pull complete                                                                                                                                                                               <span class="number">3.0</span>s</span><br><span class="line">   ⠿ <span class="number">719</span>adce26c52 Pull complete                                                                                                                                                                               <span class="number">3.1</span>s</span><br><span class="line">   ⠿ b8f35e378c31 Pull complete                                                                                                                                                                               <span class="number">3.3</span>s</span><br><span class="line">   ⠿ d034517f789c Pull complete                                                                                                                                                                               <span class="number">9.0</span>s</span><br><span class="line">   ⠿ <span class="number">3772</span>d4d76753 Pull complete                                                                                                                                                                               <span class="number">9.0</span>s</span><br><span class="line">   ⠿ <span class="number">211</span>a7f52febb Pull complete                                                                                                                                                                               <span class="number">9.1</span>s</span><br><span class="line">Sending build context to Docker daemon     <span class="number">725</span>B</span><br><span class="line">Step <span class="number">1</span>/<span class="number">10</span> : FROM python:<span class="number">3.7</span><span class="literal">-alpine</span></span><br><span class="line"><span class="number">3.7</span><span class="literal">-alpine</span>: Pulling from library/python</span><br><span class="line"><span class="number">59</span>bf1c3509f3: Already exists </span><br><span class="line"><span class="number">8786870</span>f2876: Pull complete </span><br><span class="line"><span class="number">45</span>d4696938d0: Pull complete </span><br><span class="line">ef84af58b2c5: Pull complete </span><br><span class="line">c3c9b71b9a69: Pull complete </span><br><span class="line">Digest: sha256:d64e0124674d64e78cc9d7378a1130499ced66a7a00db0521d0120a2e88ac9e4</span><br><span class="line">Status: Downloaded newer image <span class="keyword">for</span> python:<span class="number">3.7</span><span class="literal">-alpine</span></span><br><span class="line"> <span class="literal">---</span>&gt; a1034fd13493</span><br><span class="line">Step <span class="number">2</span>/<span class="number">10</span> : WORKDIR /code</span><br><span class="line"> <span class="literal">---</span>&gt; Running <span class="keyword">in</span> e23e4b173abf</span><br><span class="line">Removing intermediate container e23e4b173abf</span><br><span class="line"> <span class="literal">---</span>&gt; <span class="number">41</span>eb64157cfc</span><br><span class="line">Step <span class="number">3</span>/<span class="number">10</span> : ENV FLASK_APP=app.py</span><br><span class="line"> <span class="literal">---</span>&gt; Running <span class="keyword">in</span> cdefb769398d</span><br><span class="line">Removing intermediate container cdefb769398d</span><br><span class="line"> <span class="literal">---</span>&gt; ab741ac5cb17</span><br><span class="line">Step <span class="number">4</span>/<span class="number">10</span> : ENV FLASK_RUN_HOST=<span class="number">0.0</span>.<span class="number">0.0</span></span><br><span class="line"> <span class="literal">---</span>&gt; Running <span class="keyword">in</span> <span class="number">4976</span>c1da428c</span><br><span class="line">Removing intermediate container <span class="number">4976</span>c1da428c</span><br><span class="line"> <span class="literal">---</span>&gt; <span class="number">5</span>a5c24d67db6</span><br><span class="line">Step <span class="number">5</span>/<span class="number">10</span> : RUN apk add <span class="literal">--no-cache</span> gcc musl<span class="literal">-dev</span> linux<span class="literal">-headers</span></span><br><span class="line"> <span class="literal">---</span>&gt; Running <span class="keyword">in</span> <span class="number">53043</span>bd38e33</span><br><span class="line">fetch https://dl<span class="literal">-cdn</span>.alpinelinux.org/alpine/v3.<span class="number">15</span>/main/x86_64/APKINDEX.tar.gz</span><br><span class="line">fetch https://dl<span class="literal">-cdn</span>.alpinelinux.org/alpine/v3.<span class="number">15</span>/community/x86_64/APKINDEX.tar.gz</span><br><span class="line">(<span class="number">1</span>/<span class="number">13</span>) Installing libgcc (<span class="number">10.3</span>.<span class="number">1</span>_git20211027<span class="literal">-r0</span>)</span><br><span class="line">(<span class="number">2</span>/<span class="number">13</span>) Installing libstdc++ (<span class="number">10.3</span>.<span class="number">1</span>_git20211027<span class="literal">-r0</span>)</span><br><span class="line">(<span class="number">3</span>/<span class="number">13</span>) Installing binutils (<span class="number">2.37</span><span class="literal">-r3</span>)</span><br><span class="line">(<span class="number">4</span>/<span class="number">13</span>) Installing libgomp (<span class="number">10.3</span>.<span class="number">1</span>_git20211027<span class="literal">-r0</span>)</span><br><span class="line">(<span class="number">5</span>/<span class="number">13</span>) Installing libatomic (<span class="number">10.3</span>.<span class="number">1</span>_git20211027<span class="literal">-r0</span>)</span><br><span class="line">(<span class="number">6</span>/<span class="number">13</span>) Installing libgphobos (<span class="number">10.3</span>.<span class="number">1</span>_git20211027<span class="literal">-r0</span>)</span><br><span class="line">(<span class="number">7</span>/<span class="number">13</span>) Installing gmp (<span class="number">6.2</span>.<span class="number">1</span><span class="literal">-r0</span>)</span><br><span class="line">(<span class="number">8</span>/<span class="number">13</span>) Installing isl22 (<span class="number">0.22</span><span class="literal">-r0</span>)</span><br><span class="line">(<span class="number">9</span>/<span class="number">13</span>) Installing mpfr4 (<span class="number">4.1</span>.<span class="number">0</span><span class="literal">-r0</span>)</span><br><span class="line">(<span class="number">10</span>/<span class="number">13</span>) Installing mpc1 (<span class="number">1.2</span>.<span class="number">1</span><span class="literal">-r0</span>)</span><br><span class="line">(<span class="number">11</span>/<span class="number">13</span>) Installing gcc (<span class="number">10.3</span>.<span class="number">1</span>_git20211027<span class="literal">-r0</span>)</span><br><span class="line">(<span class="number">12</span>/<span class="number">13</span>) Installing linux<span class="literal">-headers</span> (<span class="number">5.10</span>.<span class="number">41</span><span class="literal">-r0</span>)</span><br><span class="line">(<span class="number">13</span>/<span class="number">13</span>) Installing musl<span class="literal">-dev</span> (<span class="number">1.2</span>.<span class="number">2</span><span class="literal">-r7</span>)</span><br><span class="line">Executing busybox<span class="literal">-1</span>.<span class="number">34.1</span><span class="literal">-r3</span>.trigger</span><br><span class="line">OK: <span class="number">139</span> MiB <span class="keyword">in</span> <span class="number">48</span> packages</span><br><span class="line">Removing intermediate container <span class="number">53043</span>bd38e33</span><br><span class="line"> <span class="literal">---</span>&gt; <span class="number">73</span>e9550df596</span><br><span class="line">Step <span class="number">6</span>/<span class="number">10</span> : <span class="built_in">COPY</span> requirements.txt requirements.txt</span><br><span class="line"> <span class="literal">---</span>&gt; c5a73d6f1fe1</span><br><span class="line">Step <span class="number">7</span>/<span class="number">10</span> : RUN pip install <span class="literal">-r</span> requirements.txt</span><br><span class="line"> <span class="literal">---</span>&gt; Running <span class="keyword">in</span> <span class="number">826790</span>d0bfbb</span><br><span class="line">Collecting flask</span><br><span class="line">  Downloading Flask<span class="literal">-2</span>.<span class="number">0.2</span><span class="literal">-py3-none-any</span>.whl (<span class="number">95</span> kB)</span><br><span class="line">Collecting redis</span><br><span class="line">  Downloading redis<span class="literal">-4</span>.<span class="number">1.0</span><span class="literal">-py3-none-any</span>.whl (<span class="number">171</span> kB)</span><br><span class="line">Collecting itsdangerous&gt;=<span class="number">2.0</span></span><br><span class="line">  Downloading itsdangerous<span class="literal">-2</span>.<span class="number">0.1</span><span class="literal">-py3-none-any</span>.whl (<span class="number">18</span> kB)</span><br><span class="line">Collecting click&gt;=<span class="number">7.1</span>.<span class="number">2</span></span><br><span class="line">  Downloading click<span class="literal">-8</span>.<span class="number">0.3</span><span class="literal">-py3-none-any</span>.whl (<span class="number">97</span> kB)</span><br><span class="line">Collecting Werkzeug&gt;=<span class="number">2.0</span></span><br><span class="line">  Downloading Werkzeug<span class="literal">-2</span>.<span class="number">0.2</span><span class="literal">-py3-none-any</span>.whl (<span class="number">288</span> kB)</span><br><span class="line">Collecting Jinja2&gt;=<span class="number">3.0</span></span><br><span class="line">  Downloading Jinja2<span class="literal">-3</span>.<span class="number">0.3</span><span class="literal">-py3-none-any</span>.whl (<span class="number">133</span> kB)</span><br><span class="line">Collecting packaging&gt;=<span class="number">21.3</span></span><br><span class="line">  Downloading packaging<span class="literal">-21</span>.<span class="number">3</span><span class="literal">-py3-none-any</span>.whl (<span class="number">40</span> kB)</span><br><span class="line">Collecting deprecated&gt;=<span class="number">1.2</span>.<span class="number">3</span></span><br><span class="line">  Downloading Deprecated<span class="literal">-1</span>.<span class="number">2.13</span><span class="literal">-py2</span>.py3<span class="literal">-none-any</span>.whl (<span class="number">9.6</span> kB)</span><br><span class="line">Collecting importlib<span class="literal">-metadata</span>&gt;=<span class="number">1.0</span></span><br><span class="line">  Downloading importlib_metadata<span class="literal">-4</span>.<span class="number">10.0</span><span class="literal">-py3-none-any</span>.whl (<span class="number">17</span> kB)</span><br><span class="line">Collecting wrapt&lt;<span class="number">2</span>,&gt;=<span class="number">1.10</span></span><br><span class="line">  Downloading wrapt<span class="literal">-1</span>.<span class="number">13.3</span><span class="literal">-cp37-cp37m-musllinux_1_1_x86_64</span>.whl (<span class="number">78</span> kB)</span><br><span class="line">Collecting zipp&gt;=<span class="number">0.5</span></span><br><span class="line">  Downloading zipp<span class="literal">-3</span>.<span class="number">7.0</span><span class="literal">-py3-none-any</span>.whl (<span class="number">5.3</span> kB)</span><br><span class="line">Collecting ty<span class="built_in">ping-extensions</span>&gt;=<span class="number">3.6</span>.<span class="number">4</span></span><br><span class="line">  Downloading typing_extensions<span class="literal">-4</span>.<span class="number">0.1</span><span class="literal">-py3-none-any</span>.whl (<span class="number">22</span> kB)</span><br><span class="line">Collecting MarkupSafe&gt;=<span class="number">2.0</span></span><br><span class="line">  Downloading MarkupSafe<span class="literal">-2</span>.<span class="number">0.1</span><span class="literal">-cp37-cp37m-musllinux_1_1_x86_64</span>.whl (<span class="number">30</span> kB)</span><br><span class="line">Collecting pyparsing!=<span class="number">3.0</span>.<span class="number">5</span>,&gt;=<span class="number">2.0</span>.<span class="number">2</span></span><br><span class="line">  Downloading pyparsing<span class="literal">-3</span>.<span class="number">0.6</span><span class="literal">-py3-none-any</span>.whl (<span class="number">97</span> kB)</span><br><span class="line">Installing collected packages: zipp, ty<span class="built_in">ping-extensions</span>, wrapt, pyparsing, MarkupSafe, importlib<span class="literal">-metadata</span>, Werkzeug, packaging, Jinja2, itsdangerous, deprecated, click, redis, flask</span><br><span class="line">Successfully installed Jinja2<span class="literal">-3</span>.<span class="number">0.3</span> MarkupSafe<span class="literal">-2</span>.<span class="number">0.1</span> Werkzeug<span class="literal">-2</span>.<span class="number">0.2</span> click<span class="literal">-8</span>.<span class="number">0.3</span> deprecated<span class="literal">-1</span>.<span class="number">2.13</span> flask<span class="literal">-2</span>.<span class="number">0.2</span> importlib<span class="literal">-metadata-4</span>.<span class="number">10.0</span> itsdangerous<span class="literal">-2</span>.<span class="number">0.1</span> packaging<span class="literal">-21</span>.<span class="number">3</span> pyparsing<span class="literal">-3</span>.<span class="number">0.6</span> redis<span class="literal">-4</span>.<span class="number">1.0</span> ty<span class="built_in">ping-extensions</span><span class="literal">-4</span>.<span class="number">0.1</span> wrapt<span class="literal">-1</span>.<span class="number">13.3</span> zipp<span class="literal">-3</span>.<span class="number">7.0</span></span><br><span class="line">WARNING: Running pip as the <span class="string">&#x27;root&#x27;</span> user can result <span class="keyword">in</span> broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv</span><br><span class="line">WARNING: You are <span class="keyword">using</span> pip version 21.2.4; however, version 21.3.1 is available.</span><br><span class="line">You should consider upgrading via the <span class="string">&#x27;/usr/local/bin/python -m pip install --upgrade pip&#x27;</span> command.</span><br><span class="line">Removing intermediate container <span class="number">826790</span>d0bfbb</span><br><span class="line"> <span class="literal">---</span>&gt; c1483947fad2</span><br><span class="line">Step <span class="number">8</span>/<span class="number">10</span> : EXPOSE <span class="number">5000</span></span><br><span class="line"> <span class="literal">---</span>&gt; Running <span class="keyword">in</span> cd59e0408b47</span><br><span class="line">Removing intermediate container cd59e0408b47</span><br><span class="line"> <span class="literal">---</span>&gt; <span class="number">05</span>c632dea80d</span><br><span class="line">Step <span class="number">9</span>/<span class="number">10</span> : <span class="built_in">COPY</span> . .</span><br><span class="line"> <span class="literal">---</span>&gt; <span class="number">8</span>c89b910e366</span><br><span class="line">Step <span class="number">10</span>/<span class="number">10</span> : CMD [<span class="string">&quot;flask&quot;</span>, <span class="string">&quot;run&quot;</span>]</span><br><span class="line"> <span class="literal">---</span>&gt; Running <span class="keyword">in</span> <span class="number">1</span>d9d071fee96</span><br><span class="line">Removing intermediate container <span class="number">1</span>d9d071fee96</span><br><span class="line"> <span class="literal">---</span>&gt; de4639486b50</span><br><span class="line">Successfully built de4639486b50</span><br><span class="line">Successfully tagged docker<span class="literal">-compose_web</span>:latest</span><br><span class="line">Use <span class="string">&#x27;docker scan&#x27;</span> to run Snyk tests against images to find vulnerabilities and learn how to fix them</span><br><span class="line">[+] Running <span class="number">3</span>/<span class="number">3</span></span><br><span class="line"> ⠿ Network docker<span class="literal">-compose_default</span>    Created                                                                                                                                                                  <span class="number">0.3</span>s</span><br><span class="line"> ⠿ Container docker<span class="literal">-compose-redis-1</span>  Created                                                                                                                                                                  <span class="number">0.0</span>s</span><br><span class="line"> ⠿ Container docker<span class="literal">-compose-web-1</span>    Created                                                                                                                                                                  <span class="number">0.0</span>s</span><br><span class="line">Attaching to docker<span class="literal">-compose-redis-1</span>, docker<span class="literal">-compose-web-1</span></span><br><span class="line">docker<span class="literal">-compose-redis-1</span>  | <span class="number">1</span>:C <span class="number">07</span> Jan <span class="number">2022</span> <span class="number">08</span>:<span class="number">36</span>:<span class="number">26.687</span> <span class="comment"># oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo</span></span><br><span class="line">docker<span class="literal">-compose-redis-1</span>  | <span class="number">1</span>:C <span class="number">07</span> Jan <span class="number">2022</span> <span class="number">08</span>:<span class="number">36</span>:<span class="number">26.687</span> <span class="comment"># Redis version=6.2.6, bits=64, commit=00000000, modified=0, pid=1, just started</span></span><br><span class="line">docker<span class="literal">-compose-redis-1</span>  | <span class="number">1</span>:C <span class="number">07</span> Jan <span class="number">2022</span> <span class="number">08</span>:<span class="number">36</span>:<span class="number">26.687</span> <span class="comment"># Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf</span></span><br><span class="line">docker<span class="literal">-compose-redis-1</span>  | <span class="number">1</span>:M <span class="number">07</span> Jan <span class="number">2022</span> <span class="number">08</span>:<span class="number">36</span>:<span class="number">26.687</span> * monotonic clock: POSIX clock_gettime</span><br><span class="line">docker<span class="literal">-compose-redis-1</span>  | <span class="number">1</span>:M <span class="number">07</span> Jan <span class="number">2022</span> <span class="number">08</span>:<span class="number">36</span>:<span class="number">26.688</span> * Running mode=standalone, port=<span class="number">6379</span>.</span><br><span class="line">docker<span class="literal">-compose-redis-1</span>  | <span class="number">1</span>:M <span class="number">07</span> Jan <span class="number">2022</span> <span class="number">08</span>:<span class="number">36</span>:<span class="number">26.688</span> <span class="comment"># WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.</span></span><br><span class="line">docker<span class="literal">-compose-redis-1</span>  | <span class="number">1</span>:M <span class="number">07</span> Jan <span class="number">2022</span> <span class="number">08</span>:<span class="number">36</span>:<span class="number">26.688</span> <span class="comment"># Server initialized</span></span><br><span class="line">docker<span class="literal">-compose-redis-1</span>  | <span class="number">1</span>:M <span class="number">07</span> Jan <span class="number">2022</span> <span class="number">08</span>:<span class="number">36</span>:<span class="number">26.688</span> <span class="comment"># WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add &#x27;vm.overcommit_memory = 1&#x27; to /etc/sysctl.conf and then reboot or run the command &#x27;sysctl vm.overcommit_memory=1&#x27; for this to take effect.</span></span><br><span class="line">docker<span class="literal">-compose-redis-1</span>  | <span class="number">1</span>:M <span class="number">07</span> Jan <span class="number">2022</span> <span class="number">08</span>:<span class="number">36</span>:<span class="number">26.688</span> * Ready to accept connections</span><br><span class="line">docker<span class="literal">-compose-web-1</span>    |  * Serving Flask app <span class="string">&#x27;app.py&#x27;</span> (lazy loading)</span><br><span class="line">docker<span class="literal">-compose-web-1</span>    |  * Environment: production</span><br><span class="line">docker<span class="literal">-compose-web-1</span>    |    WARNING: This is a development server. <span class="keyword">Do</span> not use it <span class="keyword">in</span> a production deployment.</span><br><span class="line">docker<span class="literal">-compose-web-1</span>    |    Use a production WSGI server instead.</span><br><span class="line">docker<span class="literal">-compose-web-1</span>    |  * Debug mode: off</span><br><span class="line">docker<span class="literal">-compose-web-1</span>    |  * Running on all addresses.</span><br><span class="line">docker<span class="literal">-compose-web-1</span>    |    WARNING: This is a development server. <span class="keyword">Do</span> not use it <span class="keyword">in</span> a production deployment.</span><br><span class="line">docker<span class="literal">-compose-web-1</span>    |  * Running on http://<span class="number">172.18</span>.<span class="number">0.2</span>:<span class="number">5000</span>/ (Press CTRL+C to quit)</span><br></pre></td></tr></table></figure>
<blockquote>
<p>查看镜像</p>
</blockquote>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">[<span class="type">root</span>@<span class="type">sail</span> <span class="type">docker</span>-<span class="type">compose</span>]<span class="comment"># docker images</span></span><br><span class="line">REPOSITORY           TAG          IMAGE ID       CREATED          SIZE</span><br><span class="line">docker<span class="literal">-compose_web</span>   latest       <span class="number">6</span>df8b78250a1   <span class="number">44</span> seconds ago   <span class="number">185</span>MB</span><br><span class="line">redis                alpine       <span class="number">3900</span>abf41552   <span class="number">5</span> weeks ago      <span class="number">32.4</span>MB</span><br><span class="line">python               <span class="number">3.7</span><span class="literal">-alpine</span>   a1034fd13493   <span class="number">5</span> weeks ago      <span class="number">41.8</span>MB</span><br></pre></td></tr></table></figure>
<p>启动 Docker Compose 时，会自动拉取需要的镜像。</p>
<blockquote>
<p>查看容器</p>
</blockquote>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">[<span class="type">root</span>@<span class="type">sail</span> <span class="type">docker</span>-<span class="type">compose</span>]<span class="comment"># docker ps</span></span><br><span class="line">CONTAINER ID   IMAGE                COMMAND                  CREATED         STATUS         PORTS                    NAMES</span><br><span class="line"><span class="number">78</span>a6f8b03a49   docker<span class="literal">-compose_web</span>   <span class="string">&quot;flask run&quot;</span>              <span class="number">8</span> minutes ago   Up <span class="number">8</span> minutes   <span class="number">0.0</span>.<span class="number">0.0</span>:<span class="number">5000</span>-&gt;<span class="number">5000</span>/tcp   docker<span class="literal">-compose-web-1</span></span><br><span class="line">b4da6da4364f   redis:alpine         <span class="string">&quot;docker-entrypoint.s…&quot;</span>   <span class="number">8</span> minutes ago   Up <span class="number">8</span> minutes   <span class="number">6379</span>/tcp                 docker<span class="literal">-compose-redis-1</span></span><br></pre></td></tr></table></figure>
<p>可以看到容器命名都带有数字，是因为需要集群管理，数字代表副本序号。</p>
<blockquote>
<p>查看网络</p>
</blockquote>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">[<span class="type">root</span>@<span class="type">sail</span> <span class="type">docker</span>-<span class="type">compose</span>]<span class="comment"># docker network ls</span></span><br><span class="line">NETWORK ID     NAME                     DRIVER    SCOPE</span><br><span class="line">b89f719e94e0   bridge                   bridge    local</span><br><span class="line"><span class="number">619</span>a5845a105   docker<span class="literal">-compose_default</span>   bridge    local</span><br><span class="line"><span class="number">28</span>d77e958643   host                     host      local</span><br><span class="line"><span class="number">801</span>fbbe1b38c   mynet                    bridge    local</span><br><span class="line">c3ff850e96f0   none                     null      local</span><br></pre></td></tr></table></figure>
<p>项目中的内容都在同个网络下。</p>
<blockquote>
<p>访问测试</p>
</blockquote>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">[<span class="type">root</span>@<span class="type">sail</span> <span class="type">docker</span>-<span class="type">compose</span>]<span class="comment"># curl localhost:5000</span></span><br><span class="line">Hello World! I have been seen <span class="number">1</span> times.</span><br><span class="line">[<span class="type">root</span>@<span class="type">sail</span> <span class="type">docker</span>-<span class="type">compose</span>]<span class="comment"># curl localhost:5000</span></span><br><span class="line">Hello World! I have been seen <span class="number">2</span> times.</span><br><span class="line">[<span class="type">root</span>@<span class="type">sail</span> <span class="type">docker</span>-<span class="type">compose</span>]<span class="comment"># curl localhost:5000</span></span><br><span class="line">Hello World! I have been seen <span class="number">3</span> times.</span><br></pre></td></tr></table></figure>
<p>Docker Compose 启动完成。</p>
<h3 id="停止"><a class="markdownIt-Anchor" href="#停止"></a> 停止</h3>
<h3 id="docker-compose-stop"><a class="markdownIt-Anchor" href="#docker-compose-stop"></a> docker-compose stop</h3>
<blockquote>
<p>示例</p>
</blockquote>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">[<span class="type">root</span>@<span class="type">sail</span> <span class="type">docker</span>-<span class="type">compose</span>]<span class="comment"># docker-compose stop</span></span><br><span class="line">[+] Running <span class="number">2</span>/<span class="number">2</span></span><br><span class="line"> ⠿ Container docker<span class="literal">-compose-redis-1</span>  Stopped                                                                       <span class="number">0.2</span>s</span><br><span class="line"> ⠿ Container docker<span class="literal">-compose-web-1</span>    Stopped</span><br></pre></td></tr></table></figure>
<h3 id="停止并删除容器和网络"><a class="markdownIt-Anchor" href="#停止并删除容器和网络"></a> 停止并删除容器和网络</h3>
<h3 id="docker-compose-down"><a class="markdownIt-Anchor" href="#docker-compose-down"></a> docker-compose down</h3>
<blockquote>
<p>示例</p>
</blockquote>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">[<span class="type">root</span>@<span class="type">sail</span> <span class="type">docker</span>-<span class="type">compose</span>]<span class="comment"># docker-compose down</span></span><br><span class="line">[+] Running <span class="number">3</span>/<span class="number">3</span></span><br><span class="line"> ⠿ Container docker<span class="literal">-compose-web-1</span>    Removed                                                                        <span class="number">10.2</span>s</span><br><span class="line"> ⠿ Container docker<span class="literal">-compose-redis-1</span>  Removed                                                                        <span class="number">0.2</span>s</span><br><span class="line"> ⠿ Network docker<span class="literal">-compose_default</span>    Removed</span><br><span class="line"> </span><br><span class="line"> [<span class="type">root</span>@<span class="type">sail</span> <span class="type">docker</span>-<span class="type">compose</span>]<span class="comment"># docker ps</span></span><br><span class="line">CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES</span><br><span class="line"></span><br><span class="line">[<span class="type">root</span>@<span class="type">sail</span> <span class="type">docker</span>-<span class="type">compose</span>]<span class="comment"># docker network ls</span></span><br><span class="line">NETWORK ID     NAME      DRIVER    SCOPE</span><br><span class="line">b89f719e94e0   bridge    bridge    local</span><br><span class="line"><span class="number">28</span>d77e958643   host      host      local</span><br><span class="line"><span class="number">801</span>fbbe1b38c   mynet     bridge    local</span><br><span class="line">c3ff850e96f0   none      null      local</span><br></pre></td></tr></table></figure>
<p>可以看出，<strong>容器</strong>和<strong>网络</strong>都被删除了。</p>
<h2 id="更多配置"><a class="markdownIt-Anchor" href="#更多配置"></a> 更多配置</h2>
<p><span class="exturl" data-url="aHR0cHM6Ly9kb2NzLmRvY2tlci5jb20vY29tcG9zZS9jb21wb3NlLWZpbGUvY29tcG9zZS1maWxlLXYzLyNjb21wb3NlLWZpbGUtc3RydWN0dXJlLWFuZC1leGFtcGxlcw==">https://docs.docker.com/compose/compose-file/compose-file-v3/#compose-file-structure-and-examples<i class="fa fa-external-link-alt"></i></span></p>

    </div>

    
    
    

    <footer class="post-footer">







<div class="license">
  <div class="license-title">7.Docker Compose</div>
  <div class="license-link">
    <a href="https://blog.fantasyke.cn/posts/61442/">https://blog.fantasyke.cn/posts/61442/</a>
  </div>
  <div class="license-meta">
    <div class="license-meta-item">
      <div class="license-meta-title">本文作者</div>
      <div class="license-meta-text">
          Fantasy-ke
      </div>
    </div>
      <div class="license-meta-item">
        <div class="license-meta-title">发布于</div>
        <div class="license-meta-text">
          2023-04-11
        </div>
      </div>
      <div class="license-meta-item">
        <div class="license-meta-title">更新于</div>
        <div class="license-meta-text">
          2024-12-17
        </div>
      </div>
    <div class="license-meta-item">
      <div class="license-meta-title">许可协议</div>
      <div class="license-meta-text">
          禁止转载引用
      </div>
    </div>
  </div>
  <div class="license-statement">
      如需转载或引用本文，请先获得作者授权！
  </div>
</div>
          <div class="reward-container">
  <div>Buy me a coffee</div>
  <button>
    Donate
  </button>
  <div class="post-reward">
      <div>
        <img src="/images/wechatpay.png" alt="Fantasy-ke WeChat Pay">
        <span>WeChat Pay</span>
      </div>

  </div>
</div>

          <div class="followme">
  <span>Welcome to my other publishing channels</span>

  <div class="social-list">

      <div class="social-item">
          <a target="_blank" class="social-link" href="https://github.com/fantasy-ke">
            <span class="icon">
              <i class="fab fa-github"></i>
            </span>

            <span class="label">GitHub</span>
          </a>
      </div>
  </div>
</div>

          <div class="post-tags">
              <a href="/tags/Docker/" rel="tag"><i class="fa fa-tag"></i> Docker</a>
          </div>

        
  <div class="social-like a2a_kit a2a_kit_size_32 a2a_default_style">
    <a class="a2a_dd" target="_blank" rel="noopener" href="https://www.addtoany.com/share"></a>
      <a class="a2a_button_facebook"></a>
      <a class="a2a_button_twitter"></a>
      <a class="a2a_button_telegram"></a>
      <a class="a2a_button_wechat"></a>
  </div>

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/posts/24227/" rel="prev" title="多线程">
                  <i class="fa fa-angle-left"></i> 多线程
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/posts/12360/" rel="next" title="6.Docker网络">
                  6.Docker网络 <i class="fa fa-angle-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






    <div class="comments gitalk-container"></div>
</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">

  <div class="copyright">
    &copy; 2021 – 
    <span itemprop="copyrightYear">2024</span>
    <span class="with-love">
      <i class="fa fa-solid fa-t-rex"></i>
    </span>
    <span class="author" itemprop="copyrightHolder">Fantasy-ke</span>
  </div>
<div class="wordcount">
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-line"></i>
    </span>
      <span>Word count total: </span>
    <span title="Word count total">72k</span>
  </span>
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
      <span>Reading time total &asymp;</span>
    <span title="Reading time total">4:21</span>
  </span>
</div>
<div class="busuanzi-count">
    <span class="post-meta-item" id="busuanzi_container_site_uv">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="Total Visitors">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-item" id="busuanzi_container_site_pv">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="Total Views">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div> 
<script src="https://cdn.jsdelivr.net/npm/moment@2.22.2/moment.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/moment-precise-range-plugin@1.3.0/moment-precise-range.min.js"></script>
<script>
  var now = new Date();
  function timer() {
    var grt= new Date("03/12/2022 13:14:21");//此处修改你的建站时间或者网站上线时间
    now.setTime(now.getTime()+250);
    days = (now - grt ) / 1000 / 60 / 60 / 24; dnum = Math.floor(days);
    hours = (now - grt ) / 1000 / 60 / 60 - (24 * dnum); hnum = Math.floor(hours);
    if(String(hnum).length ==1 ){hnum = "0" + hnum;} minutes = (now - grt ) / 1000 /60 - (24 * 60 * dnum) - (60 * hnum);
    mnum = Math.floor(minutes); if(String(mnum).length ==1 ){mnum = "0" + mnum;}
    seconds = (now - grt ) / 1000 - (24 * 60 * 60 * dnum) - (60 * 60 * hnum) - (60 * mnum);
    snum = Math.round(seconds); if(String(snum).length ==1 ){snum = "0" + snum;}
    var ages = `${dnum}天${hnum}小时${mnum}分${snum}秒`;
    div.innerHTML = `本站已安全运行 ${ages} 🐶`;
  }
  var div = document.createElement("div");
  //插入到copyright之后
  var copyright = document.querySelector(".busuanzi-count");
  document.querySelector(".footer-inner").insertBefore(div, copyright.nextSibling);
  timer();
  setInterval("timer()",1000)
</script>


<div class="footer-menu">
    <a style="color: chocolate;" href="/tags/">文章标签</a> · <a style="color: chocolate;"  href="/policy/">网站政策</a> · <a style="color: chocolate;"  href="/photos/">文章相册</a>
</div>

    </div>
  </footer>

  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>
  <div class="sidebar-dimmer"></div>
  <div class="back-to-top" role="button" aria-label="Back to top">
    <i class="fa fa-arrow-up fa-lg"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>
  <a role="button" class="book-mark-link book-mark-link-fixed"></a>

  <span class="exturl github-corner" data-url="aHR0cHM6Ly9naXRodWIuY29tL2ZhbnRhc3kta2U=" title="Follow me on GitHub" aria-label="Follow me on GitHub"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></span>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/next-theme-pjax/0.6.0/pjax.min.js" integrity="sha256-vxLn1tSKWD4dqbMRyv940UYw4sXgMtYcK6reefzZrao=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.31/fancybox/fancybox.umd.js" integrity="sha256-a+H7FYzJv6oU2hfsfDGM2Ohw/cR9v+hPfxHCLdmCrE8=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/sidebar.js"></script><script src="/js/next-boot.js"></script><script src="/js/bookmark.js"></script><script src="/js/pjax.js"></script>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/hexo-generator-searchdb/1.4.1/search.js" integrity="sha256-1kfA5uHPf65M5cphT2dvymhkuyHPQp5A53EGZOnOLmc=" crossorigin="anonymous"></script>
<script src="/js/third-party/search/local-search.js"></script>




  <script src="/js/third-party/fancybox.js"></script>


  <script src="/js/third-party/addtoany.js"></script>

  
  <script data-pjax async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>




<script src="https://cdn.jsdelivr.net/npm/darkmode-js@1.5.7/lib/darkmode-js.min.js"></script>

<script>
var options = {
  bottom: '64px',
  right: 'unset',
  left: '32px',
  time: '0.5s',
  mixColor: 'transparent',
  backgroundColor: 'transparent',
  buttonColorDark: '#100f2c',
  buttonColorLight: '#fff',
  saveInCookies: true,
  label: '🌓',
  autoMatchOsTheme: true
}
const darkmode = new Darkmode(options);
window.darkmode = darkmode;
darkmode.showWidget();
</script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/gitalk/1.8.0/gitalk.css" integrity="sha256-AJnUHL7dBv6PGaeyPQJcgQPDjt/Hn/PvYZde1iqfp8U=" crossorigin="anonymous">

<script class="next-config" data-name="gitalk" type="application/json">{"enable":true,"github_id":"fantasy-ke","repo":"fantasy-ke.github.io","client_id":"Ov23li8eCFrhnzfiGx5T","client_secret":"ce23581c2059392ebd00afd08d5ba43f767fd1b1","admin_user":"fantasy-ke","distraction_free_mode":true,"proxy":"https://cors-anywhere.azm.workers.dev/https://github.com/login/oauth/access_token","language":"zh-CN","labels":"gitalk","js":{"url":"https://cdnjs.cloudflare.com/ajax/libs/gitalk/1.8.0/gitalk.min.js","integrity":"sha256-MVK9MGD/XJaGyIghSVrONSnoXoGh3IFxLw0zfvzpxR4="},"path_md5":"c73208d58771b89909f9c4902617406a"}</script>
<script src="/js/third-party/comments/gitalk.js"></script>


<script src="/js/minigrid.min.js"></script>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script data-pjax type="text/javascript">
var album = document.querySelector(".album");
if (album) {
  // 相册列表 JSON 数据
  var imgDataPath = album.getAttribute('json-src');
  // 照片存储路径
  var imgPath = album.getAttribute('photo-src');
  // 最多显示数量
  var imgMaxNum = 50;
  // 获取窗口大小以决定图片宽度
  var windowWidth = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
  var imageWidth;

  if (windowWidth < 768) {
    imageWidth = 145; // 移动端图片宽度
  } else {
    imageWidth = 235;
  }

  // 生成相册
  var linkDataPath = imgDataPath;
  var photo = {
    page: 1,
    offset: imgMaxNum,
    init: function () {
      var that = this;
      $.getJSON(linkDataPath, function (data) {
          that.render(that.page, data);
      });
    },
    render: function (page, data) {
      var begin = (page - 1) * this.offset;
      var end = page * this.offset;
      if (begin >= data.length) return;
      var imgNameWithPattern, imgName, imageSize, imageX, imageY, li = "";
      for (var i = begin; i < end && i < data.length; i++) {
        imgNameWithPattern = data[i].path;
        imgName = data[i].name;
        imageSize = data[i].size;
        li += '<div class="card" style="width:' + imageWidth + 'px" >';
        li += '<div class="album-photo" style="height:' + imageWidth + 'px" >';
        li += '<a class="fancybox fancybox.image" href="' + imgPath + imgNameWithPattern + '" itemscope="" itemtype="http://schema.org/ImageObject" itemprop="url" data-fancybox="group" rel="group" data-caption="' + imgName + '" title="' +  imgName + '">';
        li += '<img data-src="' + imgPath + imgNameWithPattern + '" src="' + imgPath + imgNameWithPattern + '" alt="' +  imgName + '" data-loaded="true">';
        li += '</a>';
        li += '</div>';
        li += '</div>';
      }
      album.insertAdjacentHTML('beforeend', li);
      this.minigrid();
    },
    minigrid: function () {
      var grid = new Minigrid({
        container: '.album',
        item: '.card',
        gutter: 12
      });
      grid.mount();
      window.addEventListener('resize', function () {
        grid.mount();
      });
    }
  };
  photo.init();
}
</script>
<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"model":{"jsonPath":"/live2dw/assets/hijiki.model.json"},"display":{"position":"right","width":170,"height":330},"mobile":{"show":true},"react":{"opacity":0.7},"log":false,"pluginJsPath":"lib/","pluginModelPath":"assets/","pluginRootPath":"live2dw/","tagMode":false});</script></body>
</html>
